package com.example.vspm.controller;

import com.example.vspm.entity.*;
import com.example.vspm.service.*;
import com.example.vspm.util.StateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sender")
public class SenderController {

    @Autowired
    TaskFormService taskFormService;

    @Autowired
    StateUtil stateUtil;

    @Autowired
    HttpServletRequest request;//通过注解获取一个request,获取session

    @Autowired
    MeetingPlaceService meetingPlaceService;

    @Autowired
    SendTableService sendTableService;

    @Autowired
    ReceiverService receiverService;

    @Autowired
    PersonService personService;

    @Autowired
    SenderEvaluationService senderEvaluationService;

    @Autowired
    ExecutiveReportService executiveReportService;


    //派单员查看全部工单
    @RequestMapping("/listTaskForm")
    public String listTaskForm(Model model){
        List<TaskForm> taskForms = taskFormService.listTaskForm();
        model.addAttribute("list",taskForms);
        return "taskForm/listTaskForm";
    }

    //根据前端传过来工单ID查看工单详情页,方便显示会场以及接单人员的挑选,评价....
    @RequestMapping("/listTaskFormByTid/{tid}")
    public String listTaskFormByTid(@PathVariable("tid") int tid, Model model){
        List<TaskForm> taskForms = taskFormService.listTaskFormByTid(tid);
        model.addAttribute("listTaskForms",taskForms);
        //查看工单对应的会场
        List<MeetingPlace> meetingPlaces=meetingPlaceService.listMeetingPlaceByTaid(tid);
        model.addAttribute("listMeetingPlaces",meetingPlaces);
        return "工单详情页(有会场,有选择接单人员按钮)";
    }

    //此时应该有个查看接单人员的按钮,按下跳转到这里
    @RequestMapping("/listReceiver")
    public String listReceiver(Model model){
        List<Receiver> receivers=receiverService.selectReceiver();
        model.addAttribute("list",receivers);
        return "前端展现接单人员列表,每个人后面有个勾选键";
    }

    //此时应该有个挑选接单人员的按钮,按下跳到这里开始生成派单表,并传递pid3
    //这里前端最好传递来一个对象*********** 传递方式请看有道云笔记里的几种传值方式
    @RequestMapping("/selectReceiver/{tid}/{pid3}/{remark}")
    public String selectReceiver(@PathVariable("tid") int taid,
    @PathVariable("pid3") int pid3, @PathVariable("remark") String remark){
        //生成派单表
        //先根据工单id获取对应的会场id
        int mid=meetingPlaceService.getId(taid);
        //设置派单表
        SendTable sendTable=new SendTable();
        sendTable.setTaid(taid);
        sendTable.setMid(mid);
        sendTable.setPid3(pid3);
        sendTable.setRemark(remark);
        sendTableService.addSendTable(sendTable);
        //工单表更新pid3
        taskFormService.addTaskFormPID3(pid3,taid);
        //工单表状态更新为待接单
        taskFormService.updateState(stateUtil.taskFormStateSwitch("3"),taid);
//        return "选人完成,等待消息回应";
        return "taskForm/listTaskForm";
    }

    //根据前端传过来工单id查看执行报告,前端for循环
    @RequestMapping("/listExecutiveReportByTid/{tid}")
    public String listExecutiveReportByTid(@PathVariable("tid") int tid, Model model){
        List<ExecutiveReport> executiveReports = executiveReportService.listExecutiveReportByTid(tid);
        model.addAttribute("listExecutiveReports",executiveReports);
        return "展示执行报告页面";
    }

    //查看的待评价工单
    //点击开始评价按钮,将工单id传过来

    @RequestMapping("/add")
    public String add(){
        return "前端写入评价页面";
    }

    //前端写入评价后保存传到这里
    @RequestMapping("/addSenderEvaluation/{tid}/{score}")
    public String addTaskForm(SenderEvaluation senderEvaluation, @PathVariable("tid") int tid,
                              @PathVariable("score") float score, HttpSession session){
        senderEvaluationService.addSenderEvaluation(senderEvaluation);
        senderEvaluationService.addSenderEvaluationTaid(tid,senderEvaluation.getTid());
        int pid3=sendTableService.getPid3(tid);
        receiverService.updateScoreCount(pid3);
        receiverService.updateScore(score,pid3);
        taskFormService.updateState(stateUtil.taskFormStateSwitch("7"),tid);
        return "评价结束页面";
    }








}
